﻿using FytSoa.Common.Enum;
using FytSoa.Common.Utils;
using FytSoa.Domain.Crm;
using FytSoa.Domain.Erp;
using FytSoa.Sugar;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace FytSoa.Application.Operator;
/// <summary>
/// 工作台
/// </summary>
[ApiExplorerSettings(GroupName = "v1")]
public class WorkbenchService : IApplicationService
{   
    private readonly SugarRepository<CrmProject> _projectRepository;
    private readonly SugarRepository<CrmProjectTask> _projectTaskRepository;
    private readonly SugarRepository<CrmContract> _contractRepository;
    private readonly SugarRepository<CrmPayment> _paymentRepository;
    private readonly SugarRepository<CrmInvoice> _invoiceRepository;
    
    private readonly SugarRepository<ErpPurchaseApply> _purchaseApplyRepository;
    private readonly SugarRepository<ErpPurchaseOrder> _purchaseOrderRepository;
    private readonly SugarRepository<ErpPurchaseReturn> _purchaseReturnRepository;
    
    private readonly SugarRepository<ErpInstock> _inStockRepository;
    private readonly SugarRepository<ErpOutstock> _outStockRepository;
    private readonly SugarRepository<ErpTransfer> _transferRepository;
    
    private readonly SugarRepository<ErpProduceOrder> _scOrderRepository;
    private readonly SugarRepository<ErpProduceOrderCraft> _scOrderCraftRepository;
    
    private readonly SugarRepository<ErpFlowAuditLog> _flowAuditLogRepository;
    public WorkbenchService(SugarRepository<CrmProject> projectRepository
    ,SugarRepository<CrmProjectTask> projectTaskRepository
    ,SugarRepository<CrmContract> contractRepository
    ,SugarRepository<CrmPayment> paymentRepository
    ,SugarRepository<CrmInvoice> invoiceRepository
    
    ,SugarRepository<ErpPurchaseApply> purchaseApplyRepository
    ,SugarRepository<ErpPurchaseOrder> purchaseOrderRepository
    ,SugarRepository<ErpPurchaseReturn> purchaseReturnRepository
    
    ,SugarRepository<ErpInstock> inStockRepository
    ,SugarRepository<ErpOutstock> outStockRepository
    ,SugarRepository<ErpTransfer> transferRepository
    
    ,SugarRepository<ErpProduceOrder> scOrderRepository
    ,SugarRepository<ErpProduceOrderCraft> scOrderCraftRepository
    
    ,SugarRepository<ErpFlowAuditLog> flowAuditLogRepository)
    {
        _projectRepository = projectRepository;
        _projectTaskRepository = projectTaskRepository;
        _contractRepository = contractRepository;
        _paymentRepository = paymentRepository;
        _invoiceRepository = invoiceRepository;

        _purchaseApplyRepository = purchaseApplyRepository;
        _purchaseOrderRepository = purchaseOrderRepository;
        _purchaseReturnRepository = purchaseReturnRepository;

        _inStockRepository = inStockRepository;
        _outStockRepository = outStockRepository;
        _transferRepository = transferRepository;

        _scOrderRepository = scOrderRepository;
        _scOrderCraftRepository = scOrderCraftRepository;

        _flowAuditLogRepository = flowAuditLogRepository;
    }
    
    /// <summary>
    /// 获得数据统计
    /// </summary>
    /// <returns></returns>
    public async Task<OverViewDto> GetOverView()
    {
        var model=new OverViewDto
        {
            Crm =
            {
                ProJect = await _projectRepository.AsQueryable().CountAsync(m=>m.Status==CrmProjectStatusEnum.NotStarted),
                ProJectTask = await _projectTaskRepository.CountAsync(m=>m.Status==CrmProjectTaskStatusEnum.Proceed),
                Contract=await _contractRepository.CountAsync(m=>m.Status==CrmAuditEnum.NotReviewed),
                Refund=await _paymentRepository.CountAsync(m=>m.Status==CrmAuditEnum.NotReviewed),
                Invoice=await _invoiceRepository.CountAsync(m=>m.Status==CrmAuditEnum.NotReviewed)
            },
            Purchase=
            {
                Apply=await _purchaseApplyRepository.CountAsync(m=>m.Status==ErpProduceApplyEnum.NewApply),
                Order = await _purchaseOrderRepository.CountAsync(m=>m.Status==ErpAuditEnum.NotReviewed),
                Return = await _purchaseReturnRepository.CountAsync(m=>m.Status==ErpAuditEnum.NotReviewed)
            },
            Stock=
            {
                In = await _inStockRepository.CountAsync(m=>m.Status==ErpAuditEnum.NotReviewed),
                Out = await _outStockRepository.CountAsync(m=>m.Status==ErpAuditEnum.NotReviewed),
                Transfer = await _transferRepository.CountAsync(m=>m.Status==ErpAuditEnum.NotReviewed)
            },
            Manufacture =
            {
                Order=await _scOrderRepository.CountAsync(m=>m.Status==ErpAuditEnum.NotReviewed),
                Design = await _scOrderRepository.CountAsync(m=>m.Phase==ErpProducePhaseEnum.Design),
                Task=await _scOrderCraftRepository.CountAsync(m=>m.Status==0),
                Quality=await _scOrderRepository.CountAsync(m=>m.Phase==ErpProducePhaseEnum.QualityControl),
            },
            Approve =
            {
                WaitProcessing = await _flowAuditLogRepository.CountAsync(m=>m.Status==FlowAuditEnum.Await),
                Reject = await _flowAuditLogRepository.CountAsync(m=>m.Status==FlowAuditEnum.Reject)
            }
        };
        return model;
    }
    
    /// <summary>
    /// 获得资源使情况
    /// </summary>
    /// <returns></returns>
    public DeviceUse GetResourceUse()
    {
        return DeviceUtils.GetInstance().GetSystemRateInfo();
    }

    /// <summary>
    /// 获得系统信息
    /// </summary>
    /// <returns></returns>
    public dynamic GetSystemInfo()
    {
        return DeviceUtils.GetInstance().GetSystemInfo();
    }
}